package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.product.ProductAttrGroupDao;
import cn.fastoo.common.dao.product.ProductCategoryAttrgroupDao;
import cn.fastoo.common.dao.product.ProductCategoryDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryAttrgroupDao;
import cn.fastoo.common.entity.product.Product;
import cn.fastoo.common.entity.product.ProductAttrGroup;
import cn.fastoo.common.entity.product.ProductCategory;
import cn.fastoo.common.entity.product.ProductCategoryAttrgroup;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product/category/attrgroup")
public class ProductCategoryAttrgroupController {

    @Autowired
    private ProductCategoryAttrgroupDao dao;
    @Autowired
    private ProductAttrGroupDao productAttrGroupDao;
    @Autowired
    private ViewProductCategoryAttrgroupDao viewProductCategoryAttrgroupDao;

    @RequestMapping
    public String index() {
        return "/product/category/attrgroup//index";
    }


    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute ProductCategoryAttrgroup newData, QuerySearch querySearch, String op) {
        Search search = querySearch.getSearch();
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                ProductCategoryAttrgroup oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(viewProductCategoryAttrgroupDao.find(search),querySearch);
    }
    @RequestMapping("/batchAddAttrGroup")
    @ResponseBody
    public String batchAddAttrGroup(@RequestParam Long productCategoryId,@RequestParam String ids) {
        Search search = new Search().addSearchFilter("IN_id",ids);
        List<String> notInIds = dao.findProductAttrGroupIdsByProductCategoryId(productCategoryId);
        if(Collections3.isNotEmpty(notInIds)){
            search.addSearchFilter("NOTIN_id",notInIds);
        }
        List<ProductAttrGroup> productAttrGroups = productAttrGroupDao.find(search);

        if(Collections3.isNotEmpty(productAttrGroups)){
            List<ProductCategoryAttrgroup> newList = new ArrayList<ProductCategoryAttrgroup>();
            for (ProductAttrGroup ptg :productAttrGroups) {
                ProductCategoryAttrgroup newOne = new ProductCategoryAttrgroup();
                newOne.setProductAttrGroupId(ptg.getId());
                newOne.setProductCategoryId(productCategoryId);
                newList.add(newOne);
            }
            dao.batchInsert(newList);
        }
        return JsonResult.success("ok");
    }



}
